#include "LoBeamView.h"
#include "F3LampInterfaces.h"

static void lobeamCanSignalFeedback()
{
    Set_CANSIG_LeLoBeamOnSt(LocalQuery_LeLoBeamOnSt());
}

float calculateLoBeamDerateCoefficient()
{
    float coefficient = 100.0f;

    do
    {
        if(IsLoBeamNTCShortCircuit())
        {
            //Ignore
            break;
        }

        if(IsLoBeamNTCOpenCircuit())
        {
            //Ignore
            break;
        }

        coefficient = LocalQuery_LobeamBrightness_Temperature();

    } while (false);
    
    
    float coefficient_v = LocalQuery_LobeamBrightness_Voltage();
    coefficient = coefficient < coefficient_v ? coefficient : coefficient_v;

    return coefficient;
}

void execute_LoBeamView(LoBeamView* this_)
{
    lobeamCanSignalFeedback();
    
    switch(this_->model_->onOffStatus_)
    {
        case OnOff_On:
            Set_LoBeam_OnOffState(true);
            break;
        
        case OnOff_Off:
            Set_LoBeam_OnOffState(false);
            break;
            
        case OnOff_Initial:
            //ignore
            break;
        
        default:
            //ignore
            break;
    }

    float coefficient = calculateLoBeamDerateCoefficient();
    Set_Lamp_Ctrl_LobeamPwm(this_->model_->pwm_ * coefficient / 100.0f);

    Set_FanCtrl_OnOffState(coefficient > 0.0f && IsLampTurnOn(this_->model_->onOffStatus_));
}

void execute_po2LedView(Po2LedView* this_)
{
    Set_PO2_OnOffState(!IsLampTurnOn(this_->model_->onOffStatus_));
}